package mamikeji.liaowa.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import mamikeji.liaowa.R;
import mamikeji.liaowa.bean.ClockBean;

/**
 * Created by 廉冲-android开发 on 2017/5/17.
 */
public class ClockListAdapter extends BaseAdapter {
    List<ClockBean> dayListBeanList = new ArrayList<>();
    LayoutInflater inflater;
    Context context;
    OnItemClickListener mOnItemClickListener;
    public Boolean boolAnim = false;

    public ClockListAdapter() {
    }

    public ClockListAdapter(List<ClockBean> dayListBeanList, Context context) {
        this.dayListBeanList = dayListBeanList;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void setList(List<ClockBean> dayListBeanList) {
        this.dayListBeanList = dayListBeanList;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    public interface OnItemClickListener {
        void onClockClick(int position, View view);

        void onDeleteClick(int position);
    }

    public void removeItemSelected(int selected) {
        if (dayListBeanList.isEmpty()) {
            return;
        }
        dayListBeanList.remove(selected);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return dayListBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return dayListBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_clock_list, null);
            viewHolder = new ViewHolder();

            viewHolder.textView_clock_item_title = (TextView) convertView.findViewById(R.id.textView_clock_item_title);
            viewHolder.gridView_clock_item_time = (GridView) convertView.findViewById(R.id.gridView_clock_item_time);
            viewHolder.radioButton_clock_item_week_1 = (RadioButton) convertView.findViewById(R.id.radioButton_clock_item_week_1);
            viewHolder.radioButton_clock_item_week_2 = (RadioButton) convertView.findViewById(R.id.radioButton_clock_item_week_2);
            viewHolder.radioButton_clock_item_week_3 = (RadioButton) convertView.findViewById(R.id.radioButton_clock_item_week_3);
            viewHolder.radioButton_clock_item_week_4 = (RadioButton) convertView.findViewById(R.id.radioButton_clock_item_week_4);
            viewHolder.radioButton_clock_item_week_5 = (RadioButton) convertView.findViewById(R.id.radioButton_clock_item_week_5);
            viewHolder.radioButton_clock_item_week_6 = (RadioButton) convertView.findViewById(R.id.radioButton_clock_item_week_6);
            viewHolder.radioButton_clock_item_week_7 = (RadioButton) convertView.findViewById(R.id.radioButton_clock_item_week_7);
            viewHolder.imageView_clock_item_yn = (ImageView) convertView.findViewById(R.id.imageView_clock_item_yn);
            viewHolder.textView_clock_item_content = (TextView) convertView.findViewById(R.id.textView_clock_item_content);
            viewHolder.relativeLayout_clock_list_delete = (RelativeLayout) convertView.findViewById(R.id.relativeLayout_clock_list_delete);

            viewHolder.radioButtonList = new ArrayList<>();
            viewHolder.radioButtonList.add(viewHolder.radioButton_clock_item_week_1);
            viewHolder.radioButtonList.add(viewHolder.radioButton_clock_item_week_2);
            viewHolder.radioButtonList.add(viewHolder.radioButton_clock_item_week_3);
            viewHolder.radioButtonList.add(viewHolder.radioButton_clock_item_week_4);
            viewHolder.radioButtonList.add(viewHolder.radioButton_clock_item_week_5);
            viewHolder.radioButtonList.add(viewHolder.radioButton_clock_item_week_6);
            viewHolder.radioButtonList.add(viewHolder.radioButton_clock_item_week_7);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final ClockBean clockBean = dayListBeanList.get(position);

        viewHolder.textView_clock_item_title.setText(clockBean.getTypeName());

        ClockListGridAdapter clockListGridAdapter = new ClockListGridAdapter(clockBean.getTime(), context);
        viewHolder.gridView_clock_item_time.setAdapter(clockListGridAdapter);

        for (int i = 0; i < 7; i++) {
            viewHolder.radioButtonList.get(i).setChecked(false);
        }

        for (int j = 0; j < clockBean.getWeek().size(); j++) {
            if (!clockBean.getWeek().get(j).equals("") && clockBean.getWeek().get(j) != null){
                viewHolder.radioButtonList.get(Integer.parseInt(clockBean.getWeek().get(j))-2).setChecked(true);
            }
        }

        viewHolder.imageView_clock_item_yn.setImageResource(clockBean.getmActive().equals("true") ?
                R.drawable.icon_dakai_n : R.drawable.icon_guanbi);
        viewHolder.textView_clock_item_content.setText(clockBean.getTimeGuid());

        viewHolder.imageView_clock_item_yn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewHolder.imageView_clock_item_yn.setImageResource(R.drawable.icon_guanbi);
            }
        });

        viewHolder.imageView_clock_item_yn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onClockClick(position, viewHolder.imageView_clock_item_yn);
            }
        });

        viewHolder.relativeLayout_clock_list_delete.setVisibility(boolAnim ? View.VISIBLE : View.GONE);
        viewHolder.relativeLayout_clock_list_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mOnItemClickListener.onDeleteClick(position);
            }
        });
        return convertView;
    }

    public class ViewHolder {
        TextView textView_clock_item_title;
        GridView gridView_clock_item_time;
        RadioButton radioButton_clock_item_week_1;
        RadioButton radioButton_clock_item_week_2;
        RadioButton radioButton_clock_item_week_3;
        RadioButton radioButton_clock_item_week_4;
        RadioButton radioButton_clock_item_week_5;
        RadioButton radioButton_clock_item_week_6;
        RadioButton radioButton_clock_item_week_7;
        ImageView imageView_clock_item_yn;
        TextView textView_clock_item_content;

        RelativeLayout relativeLayout_clock_list_delete;

        List<RadioButton> radioButtonList;
    }


    public static String getFormatedDateTime(String dateFormat, long dateTime) {
        SimpleDateFormat sDateFormat = new SimpleDateFormat(dateFormat);
        return sDateFormat.format(new Date(dateTime + 0));
    }
}
